package com.dj.mingkong.common.util;

import java.lang.reflect.InvocationTargetException;

public enum EnumUtils {
    ;

    private EnumUtils() {
    }

    public static <T extends Enum<T>> T valueOfOrdinal(Class<T> enumType, int ordinal) {
        if (!enumType.isEnum()) {
            throw new IllegalArgumentException(enumType + "is not an enum class!");
        } else {
            T[] constants = (T[]) enumType.getEnumConstants();
            if (ordinal >= 0 && ordinal < constants.length) {
                return constants[ordinal];
            } else {
                throw new IndexOutOfBoundsException(String.format("Invalid ordinal: %d, this ordinal is beyond the arrange of %s", ordinal, enumType.getName()));
            }
        }
    }

    public static <T extends Enum<T>> T valueOfCode(Class<T> enumType, int code) {
        if (!enumType.isEnum()) {
            throw new IllegalArgumentException(enumType + "is not an enum class!");
        } else {
            try {
                return (T) enumType.getMethod("find", Integer.class).invoke(enumType, code);
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException var3) {
                throw new RuntimeException(String.format("Invalid ordinal: %d, this code is beyond the arrange of %s", code, enumType.getName()));
            }
        }
    }
}
